<?php
/**
 * Created by PhpStorm.
 * User: allen.qiang
 * Date: 15-04-07
 */

/* 活动配置
 * @package module
 * @subpackage activity
 * @author allen.qiang<allen.qiang@fistforward.com>
 * */

class activityConfig {

    private $dbca;

    public function __construct( $dbca ) {
        $this->dbca = $dbca;
    }

    /**
     * 查询用户信息
     * @param array $arg_array 参数数组
     * $arg_array = array (
     *       'sortname' => string 排序字段
     *       'sortorder' => string 排序方式
     *       'page' => int 分页页码
     *       'rp' => int 分页页数
     * )
     * @return array|null
     */
    public function searchActivityInfo( &$arg_array ) {
        global $G_SHOP;
        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //config_id
        if (isset($arg_array['config_id']) && !empty($arg_array['config_id'])) {
            $sql .= ' AND config_id = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['config_id'];
        }

        //config_code
        if (isset($arg_array['config_code']) && !empty($arg_array['config_code'])) {
            $sql .= ' AND config_code = ?';
            $params_type .= 's';
            $params_array[] = trim($arg_array['config_code']);
        }

        //config_key
        if (isset($arg_array['config_key']) && is_numeric($arg_array['config_key']) ) {
            $sql .= ' AND config_key = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['config_key'];
        }

        //config_value
        if (isset($arg_array['config_value']) && is_numeric($arg_array['config_value']) ) {
            $sql .= ' AND config_value = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['config_value'];
        }

        if ($sql) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        try {

            if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {

                //统计分类数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM activity_config ' . $sql;
                $count_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }else{

                    $arg_array['count'] = $item_count;
                    //分页bug处理
                    if ($arg_array['count'] < $arg_array['rp']) {

                        $arg_array['page'] = 1;
                    }
                    //排序
                    if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                        $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                    //分页
                    if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                        $arg_array['page'] = 1;

                    $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);
                }
            } else {

                $arg_array['count'] = 0;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            //查询分类信息
            $search_sql = 'SELECT * FROM activity_config ' . $sql;

            $temp_item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
            if (!$temp_item_array) {
                return null;
            }

            foreach($temp_item_array as &$item) {

                $item_array[] = array(
                    'uuid' => $item->uuid,
                    'timeStamp' => $item->timeStamp,
                    'creator' => $item->creator,
                    'createTime' => $item->createTime,
                    'config_id' => $item->config_id,
                    'config_code' => $item->config_code,
                    'config_key' => $item->config_key,
                    'config_value' => $item->config_value,
                    'config_remark' => $item->config_remark
                );

            }
            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }

    }

    /**
     * 新增或修改信息
     * @param array $arg_array 数组
     * @return array
     */
    public function editConfig ( $arg_array ) {

        if (!isset($arg_array['config_code'], $arg_array['config_key'], $arg_array['config_value']) || empty($arg_array['creator'])) {
            return array( 'code' => 400, 'message' => '参数错误' );
        }

        if (empty($arg_array['config_code'])) {
            return array( 'code' => 400, 'message' => 'config_code不能为空' );
        }

        if (empty($arg_array['config_key'])) {
            return array( 'code' => 400, 'message' => 'config_key不能为空' );
        }

        if (empty($arg_array['config_value'])) {
            return array( 'code' => 400, 'message' => 'config_value不能为空' );
        }

        if (empty($arg_array['config_remark'])) {
            return array( 'code' => 400, 'message' => 'config_remark不能为空' );
        }

        try {

            $this->dbca->beginTransaction();

            //检查并保存用户信息
            $config = new entity('activity_config');

            $config->config_code = $arg_array['config_code'];
            $config->config_key = $arg_array['config_key'];
            $config->config_value = $arg_array['config_value'];
            $config->config_remark = $arg_array['config_remark'];

            if ($this->dbca->fetch($config)) {

                if (isset($arg_array['uuid']) && !empty($arg_array['uuid'])) {
                    throw new Exception('没有修改内容', 404);
                }else{
                    throw new Exception('记录已存在', 404);
                }

            } else {

                if (isset($arg_array['uuid']) && !empty($arg_array['uuid'])) {
                    $config->uuid = $arg_array['uuid'];
                }

                $config->creator = $arg_array['creator'];
                $config->createTime = date('Y-m-d H:i:s');

            }

            $result = $this->dbca->save( $config );

            if (!$result) {
                throw new Exception('记录保存失败', 404);
            }

            $this->dbca->commit();

        } catch (Exception $e) {

            $this->dbca->rollBack();

            if ($e->getCode() > 0) {
                return array( 'code' => $e->getCode(), 'message' => $e->getMessage() );
            } else {
                error_log($e->getMessage());
                return array( 'code' => 500, 'message' => '系统错误' );
            }

        }

        return array( 'code' => 200, 'message' => '记录保存成功' );

    }

    /**
     * 批量删除
     * @param array $ids config_id
     * @return array
     */
    public function deleteConfig( $ids ) {

        if (!isset($ids) || !is_array($ids)) {
            return array( 'code' => 400, 'message' => '参数错误' );
        }

        try {

            if (!empty($ids)) {

                foreach($ids as &$config_id) {

                    //查询用户信息
                    $config = new entity('activity_config');
                    $config->config_id = $config_id;
                    if (!$this->dbca->fetch($config)) {
                        continue;
                    }
                    $this->dbca->delete($config);
                }
            }
        } catch (Exception $e) {

            if ($e->getCode() > 0) {
                return array( 'code' => $e->getCode(), 'message' => $e->getMessage() );
            } else {
                error_log($e->getMessage());
                return array( 'code' => 500, 'message' => '系统错误' );
            }
        }
        return array( 'code' => 200, 'message' => '删除成功' );
    }
}
